'use strict';
let PAGE_SIZE = 10; //每页的条数
const db = uniCloud.database()
const collection = db.collection('articles')
exports.main = async (event, context) => {
	let res;
	if(event.type === 'add'){
		res = addArtcile(event,context)
	}
	if(event.type === 'get'){
		res = getArtcile(event,context)
	}
	//返回数据给客户端
	return res
};

// 添加文章
async function addArtcile(event,context){
	let articleInfo = event.articleInfo;
	let pub_date =new Date().toISOString()
	articleInfo.pub_date = pub_date;
	let res = await collection.add(articleInfo)
	return res
}

// 查询文章
async function getArtcile(event,context){
	let skipNum = (event.page-1)*PAGE_SIZE;
	let res = await collection.skip(skipNum).limit(PAGE_SIZE).get();
	return res
}